<?php
namespace app\cms\controller;
use app\common\base\_Db;
use app\common\entity\RequestEntity;
use think\Controller;
use think\Db;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/4
 * Time: 15:20
 */
class Ticket extends Controller{
    /*
     * 线下出票2/退票1
     */
    public function SellTicket(){
        //获取请求参数
        $request_data = [
            $post_count = new RequestEntity('Count', '票数', true, 11),
            $post_price = new RequestEntity('Price', '票价', true, 30),
            $post_status = new RequestEntity('Status', '车票状态', true, 50),
            $post_creatid = new RequestEntity('CreatId','操作人Id', false, 30),
        ];
        _Db::Validates($request_data);
        $param_field = [
            'number'	  => $post_count->text,
            'price'		  => $post_price->text,
            'status'	  => $post_status->text,
            'oper_id'	  => $post_creatid->text,
            'oper_date'   => date('Y-m-d H:i:s',time()),
        ];
        $result = Db::table('under_line')->insert($param_field);
        if ($result===1||$result===true) {
            $array_result['State']['Code'] = 1;
            if ($post_status->text == 2){
                $array_result['State']['Message'] = '出票成功';
            }
            elseif ($post_status->text == 1){
                $array_result['State']['Message'] = '退票成功';

            }
            exit(json_encode($array_result));
        }
    }
    /*
     * 出票列表
     */
    public function OutListItem(){
        $where = 'b.type_name = "出票"';
        $result = $this->ticket($where);
        //echo Db::table('')->getLastSql();
        /*$int_total_count = Db::table('under_line')
            ->field('DATE_FORMAT(oper_date, "%Y-%m-%d") days')->where('status = 2')->group('Days,price')->count();
        $result['TotalCount'] = $int_total_count;*/
        _Db::ResponseJson($result);
    }
    /*
     * 退票列表
     */
    public function BackListItem(){
        $where = 'b.type_name = "退票"';
        $result = $this->ticket($where);
        /*$int_total_count = Db::table('under_line')
            ->field('DATE_FORMAT(oper_date, "%Y-%m-%d") days')->where('status = 1')->group('Days,price')->count();
        $result['TotalCount'] = $int_total_count;*/
        _Db::ResponseJson($result);
    }
    /*
     * 详情查询
     */
    public function ItemInfo(){
        //获取请求参数
        $request_data = [
            $post_days = new RequestEntity('Days', '日期', true, 11),
            $post_price = new RequestEntity('Price', '票价', true, 11),
            $post_status = new RequestEntity('Status', '状态', true, 11),
        ];
        _Db::Validates($request_data);
        $where = "a.oper_date between '" . $post_days->text . " 00:00:00'" . " and '" . $post_days->text . " 23:59:59'" .' and a.price = '.$post_price->text.' and a.status = '.$post_status->text;
        $param_page = input('Page', 1);
        $param_pagesize = 5;
        $result = Db::table('under_line')
            ->field(
                [
                    //'a.under_id'	=> 'Id',
                    'a.oper_date'	=> 'Days',        //操作日期
                    'a.number'	    => 'count',	      //票数
                    'a.price'		=> 'Price',       //票价
                    'a.status'		=> 'Status',      //车票状态
                    'b.type_name'   => 'TicketType',  //车票状态
                    'c.nick_name'	    => 'OperName',    //操作人
                ]
            )
            ->alias('a')
            ->join('sys_public_type b','a.status = b.type_id and b.type_classify = "车票状态"','LEFT')
            ->join('staff c','a.oper_id = c.staff_id','LEFT')
            ->where($where)
            ->limit(($param_page - 1) * $param_pagesize, $param_pagesize)
            ->order('Days desc')
            ->select();
        //echo Db::table('')->getLastSql();
        foreach ( $result as $key => $value){
            $result[$key]['Order'] = $key + 1;
        };
        $count_where = "oper_date between '" . $post_days->text . " 00:00:00'" . " and '" . $post_days->text . " 23:59:59'" .' and price = '.$post_price->text.' and status = '.$post_status->text;
        $data['tableData']=$result;
        $data['Page'] = (int)$param_page;
        $data['PageSize'] = (int)$param_pagesize;
        $int_total_count = Db::table('under_line')->where($count_where)->count();
        $data['TotalCount'] = $int_total_count;
        _Db::ResponseJson($data);
    }
    /*
     * 票务查询
     * 公共方法
     */
    public function ticket($where){
        $request_data = [
            $get_keyword = new RequestEntity('search_key', '搜索内容', false, 0)
        ];
        _Db::Validates($request_data);

        $array_data_range = explode('|', $get_keyword->text);
        if(!empty($get_keyword->text)){
            if (is_array($array_data_range) && $array_data_range[0] !== '' && $array_data_range[1] !== '') {
                $where .= " and DATE_FORMAT(a.oper_date,'%Y-%m-%d') between '" . $array_data_range[0] . "' and '" . $array_data_range[1]."'";
            }
        }
        $param_page = input('Page', 1);
        $param_pagesize = 10;
        $result = Db::table('under_line')
            ->field(
                [
                    //'a.under_id'	=> 'Id',
                    'DATE_FORMAT(a.oper_date, "%Y-%m-%d")'	=> 'Days',//操作日期
                    'SUM(a.number)'		=> 'count',	                  //票数
                    'a.price'		    => 'Price',                  //票价
                    'a.status'		=> 'Status',                  //车票状态
                    'b.type_name'   => 'TicketType',                 //车票状态
                    'c.nick_name'	    => 'OperName',                   //操作人
                ]
            )
            ->alias('a')
            ->join('sys_public_type b','a.status = b.type_id and b.type_classify = "车票状态"','LEFT')
            ->join('staff c','a.oper_id = c.staff_id','LEFT')
            ->where($where)
            ->limit(($param_page - 1) * $param_pagesize, $param_pagesize)
            ->group('Days,Price')
            ->order('Days desc')
            ->select();
        foreach ( $result as $key => $value){
            $result[$key]['Order'] = $key + 1;
        };
        $data['TotalCount'] = count(Db::table('under_line')
            ->field(
                [
                    'DATE_FORMAT(a.oper_date, "%Y-%m-%d")'	=> 'Days',//操作日期
                    'SUM(a.number)'		=> 'count',	                  //票数
                    'a.price'		    => 'Price',                  //票价
                    'a.status'		=> 'Status',                  //车票状态
                    'b.type_name'   => 'TicketType',                 //车票状态
                    'c.nick_name'	    => 'OperName',                   //操作人
                ]
            )
            ->alias('a')
            ->join('sys_public_type b','a.status = b.type_id and b.type_classify = "车票状态"','LEFT')
            ->join('staff c','a.oper_id = c.staff_id','LEFT')
            ->where($where)
            ->group('Days,Price')
            ->order('Days desc')
            ->select());
        $data['tableData']=$result;
        $data['Page'] = (int)$param_page;
        $data['PageSize'] = (int)$param_pagesize;
        return $data;
    }
}